package com.zxbit.flash.ui.activity.auth;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.zxbit.flash.R;
import com.zxbit.flash.base.BaseTitleActivity;
import com.zxbit.flash.common.AuthRouter;
import com.zxbit.flash.common.Constant;
import com.zxbit.flash.common.RouterMap;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.BankCardMvpView;
import com.zxbit.flash.presenter.BankCardPresenter;
import com.zxbit.flash.bean.BankListData;
import com.zxbit.flash.response.BankListResponse;
import com.zxbit.flash.bean.BindedCardData;
import com.zxbit.flash.response.BindedCardResponse;
import com.zxbit.flash.response.BlankResponse;
import com.zxbit.flash.bean.DictionaryItemData;
import com.zxbit.flash.response.PersonalInfoResponse;
import com.zxbit.flash.utils.StringUtils;
import com.zxbit.flash.utils.ToastUtils;
import com.zxbit.flash.view.dialog.MenuDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 银行卡认证
 */
@Route(name = "银行卡认证", path = RouterMap.BANK_AUTH_PAGE)
public class BankAuthActivity extends BaseTitleActivity implements BankCardMvpView {

    @BindView(R.id.edit_real_name)
    EditText editRealName;
    @BindView(R.id.rl_real_name)
    RelativeLayout rlRealName;
    @BindView(R.id.tv_bank_number)
    TextView tvBankNumber;
    @BindView(R.id.edit_bank_number)
    EditText editBankNumber;
    @BindView(R.id.rl_bank_number)
    RelativeLayout rlBankNumber;
    @BindView(R.id.tv_select_bank)
    TextView tvSelectBank;
    @BindView(R.id.edit_select_bank)
    EditText editSelectBank;
    @BindView(R.id.iv_select_bank)
    ImageView ivSelectBank;
    @BindView(R.id.rl_select_bank)
    RelativeLayout rlSelectBank;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;
    @BindView(R.id.root_container_view)
    LinearLayout rootContainerView;
    @Autowired(name = Constant.IS_CHANGE)
    public boolean is_change;
    private BankCardPresenter presenter;
    private ArrayList<DictionaryItemData> mBankList;
    private final String DICTIONARY_TYPE_BANK_LIST = "common_bankname";
    private MenuDialog mMenuDialog;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

    }


    @Override
    protected void onUIReady() {
        ARouter.getInstance().inject(this);
        presenter = new BankCardPresenter(this);
        presenter.getBankList();
        if (UserInfo.isShowUserInformation()) presenter.getBankInfo();

        presenter.getRealName();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_auth_bank;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.bank_card_page_title);
    }

    @OnClick({R.id.tv_next_step, R.id.edit_select_bank})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_next_step:
                submit();
                break;
            case R.id.edit_select_bank:
                pickerBank();
                break;
            default:
                break;
        }


    }

    private void submit() {
        tvNextStep.setClickable(false);
        String realName = editRealName.getText().toString().trim();

        String bankCard = editBankNumber.getText().toString().trim();
        String bankName = editSelectBank.getText().toString().trim();
        if(StringUtils.isEmpty(realName)){
            ToastUtils.showShort(BankAuthActivity.this,getStringResource(R.string.bank_card_bind_name_hint));
            tvNextStep.setClickable(true);
            return;
        }
        if(StringUtils.isEmpty(bankCard)){
            ToastUtils.showShort(BankAuthActivity.this,getStringResource(R.string.bank_card_bind_banknum_hint));
            tvNextStep.setClickable(true);
            return;
        }
        if(StringUtils.isEmpty(bankName)){
            ToastUtils.showShort(BankAuthActivity.this,getStringResource(R.string.bank_card_bind_bankname_hint));
            tvNextStep.setClickable(true);
            return;
        }
        String bankCode = editSelectBank.getTag().toString();
        presenter.saveBankCard(realName, bankCode, bankName, bankCard);
        showLoadingView();
    }

    @Override
    public void onGetDataNext(BindedCardResponse response) {
        dismissLoadingView();
        fillTextView(response.getData());
    }

    @Override
    public void onGetDataNext(BlankResponse response) {
        dismissLoadingView();
        if (response.getStatus() == Constant.HTTP_OK) {
            if (!is_change) {
                AuthRouter.getInstance().jumpNextPager(this, RouterMap.BANK_AUTH_PAGE);
            } else {
                finish();
            }
        } else {
            ToastUtils.showShort(this, response.getMessage());
        }
        tvNextStep.setClickable(true);
    }

    @Override
    public void onGetDataNext(PersonalInfoResponse response) {
        if (response.getStatus() == Constant.HTTP_OK) {
            editRealName.setText(response.getData().getRealName());
        }
    }

    @Override
    public void onGetDataNext(BankListResponse response) {
        if (response.getStatus() == Constant.HTTP_OK) {
            if (response.hasData()) {
                mBankList = new ArrayList<>();
                for (BankListData data : response.getData()) {
                    DictionaryItemData dictionaryItemData = new DictionaryItemData();
                    dictionaryItemData.setDictCode(data.getCode());
                    dictionaryItemData.setDictValue(data.getName());
                    mBankList.add(dictionaryItemData);
                }
            }
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        tvNextStep.setClickable(true);
        dismissLoadingView();
    }

    private void fillTextView(BindedCardData data) {
        if (editRealName != null && !StringUtils.isEmpty(data.getRealName())) {
            editRealName.setText(data.getRealName());
        }
        if (editSelectBank != null && !StringUtils.isEmpty(data.getBankCode())) {
            editSelectBank.setTag(data.getBankCode());
        }
        if (editBankNumber != null && !StringUtils.isEmpty(data.getBankCode())) {
            editBankNumber.setText(data.getBankCard());
        }
        if (editSelectBank != null && !StringUtils.isEmpty(data.getBankValue())) {
            editSelectBank.setText(data.getBankValue());
        }
    }


    public MenuDialog getMenuDialog() {
        if (mMenuDialog == null) {
            mMenuDialog = new MenuDialog(this, R.style.dialog_bottom_full);
        }
        return mMenuDialog;
    }

    private void pickerBank() {
        getMenuDialog().showDialog(editSelectBank, mBankList);
    }
}
